package com.zg.datapush.web;

import cn.hutool.core.bean.BeanUtil;
import com.zg.common.core.R;
import com.zg.common.core.dao.datapush.entity.DpFieldMapping;
import com.zg.datapush.vo.DpFieldMappingVo;
import com.zg.datapush.req.*;
import com.zg.datapush.service.DpFieldMappingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字段映射Controller
 * @author zg
 */
@RestController
@RequestMapping("/datapush/field-mapping")
@RequiredArgsConstructor
@Api(tags = "字段映射管理")
public class DpFieldMappingController {

    private final DpFieldMappingService fieldMappingService;

    @ApiOperation("根据任务ID查询字段映射")
    @PostMapping("/list")
    public R<List<DpFieldMappingVo>> listByTaskId(@RequestBody @Valid DpPushTaskIdReq req) {
        List<DpFieldMappingVo> result = fieldMappingService.listByTaskId(req.getId());
        return R.success(result);
    }

    @ApiOperation("新增字段映射")
    @PostMapping("/add")
    public R<DpFieldMapping> add(@RequestBody @Valid DpFieldMappingAddReq req) {
        // 转换Req到Vo
        DpFieldMappingVo vo = new DpFieldMappingVo();
        BeanUtil.copyProperties(req, vo);

        return fieldMappingService.add(vo);
    }

    @ApiOperation("更新字段映射")
    @PostMapping("/update")
    public R<DpFieldMapping> update(@RequestBody @Valid DpFieldMappingUpdateReq req) {
        // 转换Req到Vo
        DpFieldMappingVo vo = new DpFieldMappingVo();
        BeanUtil.copyProperties(req, vo);

        return fieldMappingService.update(vo);
    }

    @ApiOperation("删除字段映射")
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody @Valid DpFieldMappingIdsReq req) {
        return fieldMappingService.delete(req.getIds());
    }

    @ApiOperation("批量保存字段映射")
    @PostMapping("/batchSave")
    public R<Void> batchSave(@RequestBody @Valid DpFieldMappingBatchSaveReq req) {
        return fieldMappingService.batchSave(req.getTaskId(), req.getMappings());
    }
}
